#!/usr/bin/perl 

#
# amarokSyndication - Amarok script for syndicating (atom file format) your music to Google Pages
#
# $Id: FrmAtomSyndication.pm 2008-10-05 19:19:21Z dpino$
#
# (C) 2008  Diego Pino  <pinowsky at gmail com>
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
#

package FrmAtomSyndication;

use Config::General;

use Qt;
use Qt::isa qw(Qt::Widget);
use Qt::slots
    btnSaveClicked => [];
use Qt::attributes qw(
    chkFirefox
    chkRemember
    txtUsername
    txtPassword
    settings
    groupBox
    lblUsername
    lblPassword
    btnSave
);

sub NEW
{
    shift->SUPER::NEW(@_);

        # Read config file
    if (-e "./.conf.rc") {
        $conf_obj = new Config::General("./.conf.rc");

        %config = $conf_obj->getall;
        $username = $config{"username"};
        $password = $config{"password"};
        $use_firefox = $config{"use_firefox"};
    } else {
        # Create file
        open FILE, ">./.conf.rc";
        close FILE;
    }

    this->resize(310, 275);
    this->setCaption("Amarok Atom Syndication Settings");

    chkFirefox = Qt::CheckBox("Use Firefox Session", this);
    chkFirefox->move(140, 20);
    chkFirefox->resize(140, 20);
    chkFirefox->setChecked($use_firefox);

        # Create groupbox
    groupBox = Qt::GroupBox("Google Pages", this);
    groupBox->move(20, 50);
    groupBox->resize(260, 170);

        # Create chkRememberMe
    chkRemember = Qt::CheckBox("Remember me", groupBox);
    chkRemember->move(130, 140);
    chkRemember->resize(110, 20);
    chkRemember->setChecked($remember);

        # Create labelUsername
    lblUsername= Qt::Label("Username", groupBox);
    lblUsername->move(50, 30);
    lblUsername->resize(66, 20);

        # Create txtUsername
    txtUsername = Qt::LineEdit("", groupBox);
    txtUsername ->move(50, 50);
    txtUsername ->resize(190, 22);
    if ($remember) {
        txtUsername ->setText($username);
    }

    lblPassword= Qt::Label("Password", groupBox);
    lblPassword->move(50, 90);
    lblPassword->resize(66, 20);

        # Create txtPassword
    txtPassword = Qt::LineEdit("", groupBox);
    txtPassword ->move(50, 110);
    txtPassword ->resize(190, 22);
        # EchoMode => Qt::LineEdit::Password
    txtPassword ->setEchoMode(2);
    if ($remember) {
        txtPassword->setText($password);
    }

        # Create btnSend
    btnSave = Qt::PushButton("Go!", this);
    btnSave->move(170, 230);
    btnSave->resize(110, 24);
    btnSave->connect(btnSave, SIGNAL('clicked()'),  this, SLOT('btnSaveClicked()'));
}

sub btnSaveClicked
{
    my $use_firefox = (chkFirefox->isChecked()) ? 1 : 0;
    my $remember = (chkRemember->isChecked()) ? 1 : 0;
    my $username = txtUsername->text();
    my $password = txtPassword->text();

    if (!$use_firefox && ($username eq "" || $password eq "") ) {
        my $error = Qt::MessageBox(this, "Error");
        $error->critical(this, "Error", "Username and password are required", 1, 0, 0);

        return;
    } 
        # Save conf file
    if ($remember) {
        $conf_obj = new Config::General("./.conf.rc");
        %config = $conf_obj->getall;

        $config{"username"} = $username;
        $config{"password"} = $password;
        $config{"use_firefox"} = $use_firefox;
        $config{"remember"} = $remember;

        $conf_obj->save_file("./.conf.rc", \%config);
    }

        # Store settings
    @settings = ($username, $password, $use_firefox);
    settings = \@settings;

    this->close();
}

sub getSettings()
{
    return settings;
}

1;
